#!/bin/bash

list=`ls $CONFS_DIR/$REF_DIR/*.ctl_tab | sed 's/\(.*\)\..*/\1/'`

sed 's;ref_dir;'${REF_DIR}';g' $SCRIPTS_DIR/create_enum_dictionary.sql > $X_TMP_DIR/$REF_DIR\_create_enum_dictionary_tmp.sql
sed 's;ETL_HOME;'${ETL_HOME}';g' $X_TMP_DIR/$REF_DIR\_create_enum_dictionary_tmp.sql > $X_TMP_DIR/$REF_DIR\_create_enum_dictionary.sql

sqlplus $TARGET_SCHEMA @$X_TMP_DIR/$REF_DIR\_create_enum_dictionary.sql


function load_table 
{
echo '-------------------------------------'
echo $1 '   ' $2
echo '-------------------------------------'
$SCRIPTS_DIR/enum_replace.sh `basename $1`.ctl_tab $ETL_HOME $REF_DIR $X_TMP_DIR 

 mknod $ETL_HOME/$REF_DIR.dat$2 p

 export myselect=`cat $1.ctl_tab.final  | grep '\-\-SQL:' | sed 's/--SQL://g'`
 export mydirect=`cat $1.ctl_tab.final  | grep '\-\-DIRECT:' | sed 's/--DIRECT://g'`
 export incremental=`cat $1.ctl_tab.final  | grep '\-\-INCREMENTAL:' | sed 's/--INCREMENTAL://g'`
 incremental_where=""

 if [ "$incremental" = 'Y' ]; then
   export incremental_where=`cat $1.ctl_tab.final  | grep '\-\-INCREMENTAL_WHERE:' | sed 's/--INCREMENTAL_WHERE://g'`
   export incremental_value=`cat $1.ctl_tab.final  | grep '\-\-INCREMENTAL_VALUE:' | sed 's/--INCREMENTAL_VALUE://g'`

   myvalue=`echo "set head off; 
     SET SQLPROMPT '';
      $incremental_value; 
      exit;" | sqlplus -s $TARGET_SCHEMA`
      myvalue=`echo "$myvalue" | tr "\n" " " `

      incremental_where=`echo "$incremental_where" | sed -e 's/__value__/IFNULL(\"'"$myvalue"'\",0)/'`

  else
   sqlplus $TARGET_SCHEMA @$SCRIPTS_DIR/truncate.sql `cat $1.ctl_tab  | grep 'into table' | sed 's/into table//g'`
  fi

  echo "SQL:" "${myselect} ${incremental_where}"
  mysql $MYSQL_SCHEMA --host=$MYSQL_HOST --user=$MYSQL_UNAME --password=$MYSQL_PASSWD --quick --default-character-set=utf8 -e "${myselect} ${incremental_where}" > $ETL_HOME/$REF_DIR.dat$2&


  sqlldr $TARGET_SCHEMA control=$1.ctl_tab.final DATA=$ETL_HOME/$REF_DIR.dat$2 DIRECT=$mydirect LOG=$LOG_DIR/$REF_DIR/tab\_`basename $1`.log COLUMNARRAYROWS=100 SKIP=1 ROWS=100000 BINDSIZE=100000000 
 rm -f $ETL_HOME/$REF_DIR.dat$2

} 
rm $ETL_HOME/$REF_DIR.dat*
mygroup=0
for i in $list
do	
	  let mygroup=mygroup+1
	  load_table $i $mygroup
	  # load_table $i $mygroup& nor works because of sqlldr parallel load
	  if [ $mygroup = 5 ]; then
		wait
		export mygroup=0
         fi
done

echo '-------------------------------------'
echo 'analyse schema'
echo '-------------------------------------'

sqlplus $TARGET_SCHEMA @$SCRIPTS_DIR/analyse_schema.sql $TARGET_SCHEMA
